package com.bigger.arch.app.biz.impl;

import com.bigger.arch.app.biz.IDemoBiz;
import com.bigger.arch.app.dto.UserDto;
import com.bigger.arch.app.repository.dao.DemoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.sql.Date;

@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
//因为DemoController引用属性DemoBiz，而DemoController是单例的，如果不设置代理模式模式，DemoBiz即时设置了Scope，对应单例DemoController的属性也是单例的。
public class DemoBizImpl implements IDemoBiz {

    public DemoBizImpl() {
        System.out.println("IDemoBiz 初始化");
    }

    @Autowired
    private DemoDao demoDao;

    public String hello() {
        return "IDemoBiz Hello World";
    }

    public UserDto getUser() {
        UserDto userDto = new UserDto();
        userDto.setName("郑廷建");
        userDto.setAge(28);
        userDto.setCreateDate(new Date(System.currentTimeMillis()));
        return userDto;
    }

    public void saveUser() {
        demoDao.saveUser();
    }


    private int singleIndex = 0;

    public String singletonTest() {
        singleIndex++;
        return "单例测试：" + singleIndex;
    }
}
